layui.use(['form', 'table', 'laydate', 'layarea', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate;


    /*时间*/
    laydate.render({
        elem: '#create_time',
        type:'date',
        range: true,
        done: function(value, date, endDate){
             submitInfo(value);
        }
    });

    /*列表*/
    table.render({
        elem: '#currentTableId',
        url: '/admin/IncomeData/index?action=ajax',
        method: 'post',
        // where: {mark: "{$mark}", address: "{$address}", level: "{$level}", age: "{$age}",'team':"{$team}"},
        parseData: function (res) {
            console.log(res.datacount.data.today_incomecountwx)
            // return false
            //今日收入
            $('#todayIncomeNum').html(res.datacount.data.today_incomecount);
            $('#todayIncomeNumWx').html(res.datacount.data.today_incomecountwx);
            $('#todayIncomeNumAli').html(res.datacount.data.today_incomecountali);
            //昨日收入
            $('#yesterdayIncomeNum').html(res.datacount.data.yesterday_incomecount);
            $('#yesterdayIncomeNumWx').html(res.datacount.data.yesterday_incomecountwx);
            $('#yesterdayIncomeNumAli').html(res.datacount.data.yesterday_incomecountali);

            //本月收入
            $('#monthIncomeNum').html(res.datacount.data.month_incomecount);
            $('#monthIncomeNumWx').html(res.datacount.data.month_incomecountwx);
            $('#monthIncomeNumAli').html(res.datacount.data.month_incomecountali);

            //上月收入
            $('#lastmonthIncomeNum').html(res.datacount.data.lastmonth_incomecount);
            $('#lastmonthIncomeNumWx').html(res.datacount.data.lastmonth_incomecountwx);
            $('#lastmonthIncomeNumAli').html(res.datacount.data.lastmonth_incomecountali);
            //支出
            $('#totalOutNum').html(res.datacount.data.total_outcount);
            $('#monthOutNum').html(res.datacount.data.month_outcount);
            $('#historyOutNum').html(res.datacount.data.history_outcount);
            //筛选结果

            $('#tmpTotalIncomeNum').html(res.tempcount.data.total_income);
            $('#tmpWxIncomeNum').html(res.tempcount.data.total_wx);
            $('#tmpAlilIncomeNum').html(res.tempcount.data.total_ali);
            $('#tmpTotalOutNum').html(res.tempcount.data.total_out);

        },
        // toolbar: '#toolbarDemo',
        defaultToolbar: [ ],
        cols: [[
            {field: 'create_time', title: '时间'},
            {field: 'type_title', title: '类型', },
            {field: 'total_price', title: '金额',},
            {field: 'pay_title', title: '支付方式', },
            {field: 'desc', title: '备注', },
            {title: '操作', toolbar: '#currentTableBar'}
        ]],
        limits: [10, 15, 20, 25, 30, 50, 100],
        limit: 10,
        page: true,
        skin: 'line,row',
        done: function (res, curr, count) {

        }
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;


        if (obj.event === 'edit') {

            layer.prompt({
                formType: 2,
                value: [data.desc],
                title: '请输入值',
                area: ['350px', '100px'] ,
            }, function(value, index, elem){
                $.ajax({
                    url: '/admin/IncomeData/SaveData?action=ajax',
                    type: 'post',
                    dataType: 'json',
                    data:{id:data.id,desc:value},
                    success: function(res) {

                        if (res.status == true) {
                            layer.msg('保存成功', {icon: 1, time: 1000},
                                function(){
                                    parent.location.reload();
                                })
                            layer.close(index);
                        }
                    },
                    error: function(re) {

                        layer.open({
                            title: '错误信息',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });

            })

            return false;
        }


    });



    // 监听搜索操作

    form.on('select(selectInfo)', function (data) {
        submitInfo();
    });


    // $("input[name='create_time']").on('blur',function (re) {
    //     submitInfo();
    // })

    function submitInfo(value='1') {

        var param = {};
        var t = $('#formInfo [name]');

        $.each(t, function () {
            param[this.name] = this.value;
        });
        if(value.length>1){
            param['create_time']=value;
        }
        //console.log(param)
        //return false
        //var result = JSON.stringify(d);

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                //searchParams: result
                param
            }
        }, 'data');
        return false;
    };
});
